{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 15.5 \u4ece\u6269\u5c55\u6a21\u5757\u4e2d\u5b9a\u4e49\u548c\u5bfc\u51faC\u7684API\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u6709\u4e00\u4e2aC\u6269\u5c55\u6a21\u5757\uff0c\u5728\u5185\u90e8\u5b9a\u4e49\u4e86\u5f88\u591a\u6709\u7528\u7684\u51fd\u6570\uff0c\u4f60\u60f3\u5c06\u5b83\u4eec\u5bfc\u51fa\u4e3a\u4e00\u4e2a\u516c\u5171\u7684C API\u4f9b\u5176\u4ed6\u5730\u65b9\u4f7f\u7528\u3002\n\u4f60\u60f3\u5728\u5176\u4ed6\u6269\u5c55\u6a21\u5757\u4e2d\u4f7f\u7528\u8fd9\u4e9b\u51fd\u6570\uff0c\u4f46\u662f\u4e0d\u77e5\u9053\u600e\u6837\u5c06\u5b83\u4eec\u94fe\u63a5\u8d77\u6765\uff0c\n\u5e76\u4e14\u901a\u8fc7C\u7f16\u8bd1\u5668/\u94fe\u63a5\u5668\u6765\u505a\u770b\u4e0a\u53bb\u7279\u522b\u590d\u6742\uff08\u6216\u8005\u4e0d\u53ef\u80fd\u505a\u5230\uff09\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u672c\u8282\u4e3b\u8981\u95ee\u9898\u662f\u5982\u4f55\u5904\u740615.4\u5c0f\u8282\u4e2d\u63d0\u5230\u7684Point\u5bf9\u8c61\u3002\u4ed4\u7ec6\u56de\u4e00\u4e0b\uff0c\u5728C\u4ee3\u7801\u4e2d\u5305\u542b\u4e86\u5982\u4e0b\u8fd9\u4e9b\u5de5\u5177\u51fd\u6570\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "/* Destructor function for points */\nstatic void del_Point(PyObject *obj) {\n\n  free(PyCapsule_GetPointer(obj,\"Point\"));\n}\n\n/* Utility functions */\nstatic Point *PyPoint_AsPoint(PyObject *obj) {\n  return (Point *) PyCapsule_GetPointer(obj, \"Point\");\n}\n\nstatic PyObject *PyPoint_FromPoint(Point *p, int must_free) {\n  return PyCapsule_New(p, \"Point\", must_free ? del_Point : NULL);\n}"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u73b0\u5728\u7684\u95ee\u9898\u662f\u600e\u6837\u5c06 PyPoint_AsPoint() \u548c Point_FromPoint() \u51fd\u6570\u4f5c\u4e3aAPI\u5bfc\u51fa\uff0c\n\u8fd9\u6837\u5176\u4ed6\u6269\u5c55\u6a21\u5757\u80fd\u4f7f\u7528\u5e76\u94fe\u63a5\u5b83\u4eec\uff0c\u6bd4\u5982\u5982\u679c\u4f60\u6709\u5176\u4ed6\u6269\u5c55\u4e5f\u60f3\u4f7f\u7528\u5305\u88c5\u7684Point\u5bf9\u8c61\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8981\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u9996\u5148\u8981\u4e3a sample \u6269\u5c55\u5199\u4e2a\u65b0\u7684\u5934\u6587\u4ef6\u540d\u53eb pysample.h \uff0c\u5982\u4e0b\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "/* pysample.h */\n#include \"Python.h\"\n#include \"sample.h\"\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n/* Public API Table */\ntypedef struct {\n  Point *(*aspoint)(PyObject *);\n  PyObject *(*frompoint)(Point *, int);\n} _PointAPIMethods;\n\n#ifndef PYSAMPLE_MODULE\n/* Method table in external module */\nstatic _PointAPIMethods *_point_api = 0;\n\n/* Import the API table from sample */\nstatic int import_sample(void) {\n  _point_api = (_PointAPIMethods *) PyCapsule_Import(\"sample._point_api\",0);\n  return (_point_api != NULL) ? 1 : 0;\n}\n\n/* Macros to implement the programming interface */\n#define PyPoint_AsPoint(obj) (_point_api->aspoint)(obj)\n#define PyPoint_FromPoint(obj) (_point_api->frompoint)(obj)\n#endif\n\n#ifdef __cplusplus\n}\n#endif"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u91cc\u6700\u91cd\u8981\u7684\u90e8\u5206\u662f\u51fd\u6570\u6307\u9488\u8868 _PointAPIMethods .\n\u5b83\u4f1a\u5728\u5bfc\u51fa\u6a21\u5757\u65f6\u88ab\u521d\u59cb\u5316\uff0c\u7136\u540e\u5bfc\u5165\u6a21\u5757\u65f6\u88ab\u67e5\u627e\u5230\u3002\n\u4fee\u6539\u539f\u59cb\u7684\u6269\u5c55\u6a21\u5757\u6765\u586b\u5145\u8868\u683c\u5e76\u5c06\u5b83\u50cf\u4e0b\u9762\u8fd9\u6837\u5bfc\u51fa\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "/* pysample.c */\n\n#include \"Python.h\"\n#define PYSAMPLE_MODULE\n#include \"pysample.h\"\n\n...\n/* Destructor function for points */\nstatic void del_Point(PyObject *obj) {\n  printf(\"Deleting point\\n\");\n  free(PyCapsule_GetPointer(obj,\"Point\"));\n}\n\n/* Utility functions */\nstatic Point *PyPoint_AsPoint(PyObject *obj) {\n  return (Point *) PyCapsule_GetPointer(obj, \"Point\");\n}\n\nstatic PyObject *PyPoint_FromPoint(Point *p, int free) {\n  return PyCapsule_New(p, \"Point\", free ? del_Point : NULL);\n}\n\nstatic _PointAPIMethods _point_api = {\n  PyPoint_AsPoint,\n  PyPoint_FromPoint\n};\n...\n\n/* Module initialization function */\nPyMODINIT_FUNC\nPyInit_sample(void) {\n  PyObject *m;\n  PyObject *py_point_api;\n\n  m = PyModule_Create(&samplemodule);\n  if (m == NULL)\n    return NULL;\n\n  /* Add the Point C API functions */\n  py_point_api = PyCapsule_New((void *) &_point_api, \"sample._point_api\", NULL);\n  if (py_point_api) {\n    PyModule_AddObject(m, \"_point_api\", py_point_api);\n  }\n  return m;\n}"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6700\u540e\uff0c\u4e0b\u9762\u662f\u4e00\u4e2a\u65b0\u7684\u6269\u5c55\u6a21\u5757\u4f8b\u5b50\uff0c\u7528\u6765\u52a0\u8f7d\u5e76\u4f7f\u7528\u8fd9\u4e9bAPI\u51fd\u6570\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "/* ptexample.c */\n\n/* Include the header associated with the other module */\n#include \"pysample.h\"\n\n/* An extension function that uses the exported API */\nstatic PyObject *print_point(PyObject *self, PyObject *args) {\n  PyObject *obj;\n  Point *p;\n  if (!PyArg_ParseTuple(args,\"O\", &obj)) {\n    return NULL;\n  }\n\n  /* Note: This is defined in a different module */\n  p = PyPoint_AsPoint(obj);\n  if (!p) {\n    return NULL;\n  }\n  printf(\"%f %f\\n\", p->x, p->y);\n  return Py_BuildValue(\"\");\n}\n\nstatic PyMethodDef PtExampleMethods[] = {\n  {\"print_point\", print_point, METH_VARARGS, \"output a point\"},\n  { NULL, NULL, 0, NULL}\n};\n\nstatic struct PyModuleDef ptexamplemodule = {\n  PyModuleDef_HEAD_INIT,\n  \"ptexample\",           /* name of module */\n  \"A module that imports an API\",  /* Doc string (may be NULL) */\n  -1,                 /* Size of per-interpreter state or -1 */\n  PtExampleMethods       /* Method table */\n};\n\n/* Module initialization function */\nPyMODINIT_FUNC\nPyInit_ptexample(void) {\n  PyObject *m;\n\n  m = PyModule_Create(&ptexamplemodule);\n  if (m == NULL)\n    return NULL;\n\n  /* Import sample, loading its API functions */\n  if (!import_sample()) {\n    return NULL;\n  }\n\n  return m;\n}"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u7f16\u8bd1\u8fd9\u4e2a\u65b0\u6a21\u5757\u65f6\uff0c\u4f60\u751a\u81f3\u4e0d\u9700\u8981\u53bb\u8003\u8651\u600e\u6837\u5c06\u51fd\u6570\u5e93\u6216\u4ee3\u7801\u8ddf\u5176\u4ed6\u6a21\u5757\u94fe\u63a5\u8d77\u6765\u3002\n\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u50cf\u4e0b\u9762\u8fd9\u6837\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684 setup.py \u6587\u4ef6\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# setup.py\nfrom distutils.core import setup, Extension\n\nsetup(name='ptexample',\n      ext_modules=[\n        Extension('ptexample',\n                  ['ptexample.c'],\n                  include_dirs = [],  # May need pysample.h directory\n                  )\n        ]\n)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4e00\u5207\u6b63\u5e38\uff0c\u4f60\u4f1a\u53d1\u73b0\u4f60\u7684\u65b0\u6269\u5c55\u51fd\u6570\u80fd\u548c\u5b9a\u4e49\u5728\u5176\u4ed6\u6a21\u5757\u4e2d\u7684C API\u51fd\u6570\u4e00\u8d77\u8fd0\u884c\u7684\u5f88\u597d\u3002"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import sample\np1 = sample.Point(2,3)\np1"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import ptexample\nptexample.print_point(p1)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u672c\u8282\u57fa\u4e8e\u4e00\u4e2a\u524d\u63d0\u5c31\u662f\uff0c\u80f6\u56ca\u5bf9\u8c61\u80fd\u83b7\u53d6\u4efb\u4f55\u4f60\u60f3\u8981\u7684\u5bf9\u8c61\u7684\u6307\u9488\u3002\n\u8fd9\u6837\u7684\u8bdd\uff0c\u5b9a\u4e49\u6a21\u5757\u4f1a\u586b\u5145\u4e00\u4e2a\u51fd\u6570\u6307\u9488\u7684\u7ed3\u6784\u4f53\uff0c\u521b\u5efa\u4e00\u4e2a\u6307\u5411\u5b83\u7684\u80f6\u56ca\uff0c\u5e76\u5728\u4e00\u4e2a\u6a21\u5757\u7ea7\u5c5e\u6027\u4e2d\u4fdd\u5b58\u8fd9\u4e2a\u80f6\u56ca\uff0c\n\u4f8b\u5982 sample._point_api ."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5176\u4ed6\u6a21\u5757\u80fd\u591f\u5728\u5bfc\u5165\u65f6\u83b7\u53d6\u5230\u8fd9\u4e2a\u5c5e\u6027\u5e76\u63d0\u53d6\u5e95\u5c42\u7684\u6307\u9488\u3002\n\u4e8b\u5b9e\u4e0a\uff0cPython\u63d0\u4f9b\u4e86 PyCapsule_Import() \u5de5\u5177\u51fd\u6570\uff0c\u4e3a\u4e86\u5b8c\u6210\u6240\u6709\u7684\u6b65\u9aa4\u3002\n\u4f60\u53ea\u9700\u63d0\u4f9b\u5c5e\u6027\u7684\u540d\u5b57\u5373\u53ef\uff08\u6bd4\u5982sample._point_api\uff09\uff0c\u7136\u540e\u4ed6\u5c31\u4f1a\u4e00\u6b21\u6027\u627e\u5230\u80f6\u56ca\u5bf9\u8c61\u5e76\u63d0\u53d6\u51fa\u6307\u9488\u6765\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728\u5c06\u88ab\u5bfc\u51fa\u51fd\u6570\u53d8\u4e3a\u5176\u4ed6\u6a21\u5757\u4e2d\u666e\u901a\u51fd\u6570\u65f6\uff0c\u6709\u4e00\u4e9bC\u7f16\u7a0b\u9677\u9631\u9700\u8981\u6307\u51fa\u6765\u3002\n\u5728 pysample.h \u6587\u4ef6\u4e2d\uff0c\u4e00\u4e2a _point_api \u6307\u9488\u88ab\u7528\u6765\u6307\u5411\u5728\u5bfc\u51fa\u6a21\u5757\u4e2d\u88ab\u521d\u59cb\u5316\u7684\u65b9\u6cd5\u8868\u3002\n\u4e00\u4e2a\u76f8\u5173\u7684\u51fd\u6570 import_sample() \u88ab\u7528\u6765\u6307\u5411\u80f6\u56ca\u5bfc\u5165\u5e76\u521d\u59cb\u5316\u8fd9\u4e2a\u6307\u9488\u3002\n\u8fd9\u4e2a\u51fd\u6570\u5fc5\u987b\u5728\u4efb\u4f55\u51fd\u6570\u88ab\u4f7f\u7528\u4e4b\u524d\u88ab\u8c03\u7528\u3002\u901a\u5e38\u6765\u8bb2\uff0c\u5b83\u4f1a\u5728\u6a21\u5757\u521d\u59cb\u5316\u65f6\u88ab\u8c03\u7528\u5230\u3002\n\u6700\u540e\uff0cC\u7684\u9884\u5904\u7406\u5b8f\u88ab\u5b9a\u4e49\uff0c\u88ab\u7528\u6765\u901a\u8fc7\u65b9\u6cd5\u8868\u53bb\u5206\u53d1\u8fd9\u4e9bAPI\u51fd\u6570\u3002\n\u7528\u6237\u53ea\u9700\u8981\u4f7f\u7528\u8fd9\u4e9b\u539f\u59cb\u51fd\u6570\u540d\u79f0\u5373\u53ef\uff0c\u4e0d\u9700\u8981\u901a\u8fc7\u5b8f\u53bb\u4e86\u89e3\u5176\u4ed6\u4fe1\u606f\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6700\u540e\uff0c\u8fd8\u6709\u4e00\u4e2a\u91cd\u8981\u7684\u539f\u56e0\u8ba9\u4f60\u53bb\u4f7f\u7528\u8fd9\u4e2a\u6280\u672f\u6765\u94fe\u63a5\u6a21\u5757\u2014\u2014\u5b83\u975e\u5e38\u7b80\u5355\u5e76\u4e14\u53ef\u4ee5\u4f7f\u5f97\u5404\u4e2a\u6a21\u5757\u5f88\u6e05\u6670\u7684\u89e3\u8026\u3002\n\u5982\u679c\u4f60\u4e0d\u60f3\u4f7f\u7528\u672c\u673a\u7684\u6280\u672f\uff0c\u90a3\u4f60\u5c31\u5fc5\u987b\u4f7f\u7528\u5171\u4eab\u5e93\u7684\u9ad8\u7ea7\u7279\u6027\u548c\u52a8\u6001\u52a0\u8f7d\u5668\u6765\u94fe\u63a5\u6a21\u5757\u3002\n\u4f8b\u5982\uff0c\u5c06\u4e00\u4e2a\u666e\u901a\u7684API\u51fd\u6570\u653e\u5165\u4e00\u4e2a\u5171\u4eab\u5e93\u5e76\u786e\u4fdd\u6240\u6709\u6269\u5c55\u6a21\u5757\u94fe\u63a5\u5230\u90a3\u4e2a\u5171\u4eab\u5e93\u3002\n\u8fd9\u79cd\u65b9\u6cd5\u786e\u5b9e\u53ef\u884c\uff0c\u4f46\u662f\u5b83\u76f8\u5bf9\u7e41\u7410\uff0c\u7279\u522b\u662f\u5728\u5927\u578b\u7cfb\u7edf\u4e2d\u3002\n\u672c\u8282\u6f14\u793a\u4e86\u5982\u4f55\u901a\u8fc7Python\u7684\u666e\u901a\u5bfc\u5165\u673a\u5236\u548c\u4ec5\u4ec5\u51e0\u4e2a\u80f6\u56ca\u8c03\u7528\u6765\u5c06\u591a\u4e2a\u6a21\u5757\u94fe\u63a5\u8d77\u6765\u7684\u9b54\u6cd5\u3002\n\u5bf9\u4e8e\u6a21\u5757\u7684\u7f16\u8bd1\uff0c\u4f60\u53ea\u9700\u8981\u5b9a\u4e49\u5934\u6587\u4ef6\uff0c\u800c\u4e0d\u9700\u8981\u8003\u8651\u51fd\u6570\u5e93\u7684\u5185\u90e8\u7ec6\u8282\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u66f4\u591a\u5173\u4e8e\u5229\u7528C API\u6765\u6784\u9020\u6269\u5c55\u6a21\u5757\u7684\u4fe1\u606f\u53ef\u4ee5\u53c2\u8003\nPython\u7684\u6587\u6863"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.1"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": true,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}